package com.example.zzf0521;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.zzf0521.fragments.AlbumDetailFragment;
import com.example.zzf0521.fragments.AlbumListFragment;
import com.example.zzf0521.pojo.Album;

import java.util.List;

public class AlbumAdpater extends ArrayAdapter<Album> {
//    private  List<String> albums;
    private int resource;
    public AlbumAdpater(@NonNull Context context, int resource, @NonNull List<Album> albums) {
        super(context, resource, albums);
        this.resource=resource;
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Album album=getItem(position);
        convertView= LayoutInflater.from(getContext()).inflate(resource,parent,false);
        TextView tv =convertView.findViewById(R.id.name);
        TextView priveTv=convertView.findViewById(R.id.priceTv);
        Button detailBtn=convertView.findViewById(R.id.detailBtn);
        detailBtn.setTag(album);

        tv.setText(album.getName());
        priveTv.setText(album.getPrice()+"");

        detailBtn.setOnClickListener(v->switchDetail(v));

        return convertView;
    }

    private void switchDetail(View v){
        Bundle bundle=new Bundle();
        Album album=(Album) v.getTag();

        bundle.putInt("id",album.getId());
        AppCompatActivity activity=(AppCompatActivity) getContext();
        activity.getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, AlbumDetailFragment.class,bundle)
                .addToBackStack(null)
                .commit();

    }

}
